package com.test.prolenders.fragment_prolenders.main_prolenders;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.prolenders.R;
import com.test.prolenders.activity_prolenders.FeedbackActivity_prolenders;
import com.test.prolenders.activity_prolenders.SettingActivity_prolenders;
import com.test.prolenders.base_prolenders.BaseFragment_prolenders;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.dialog_prolenders.CustomerServiceDialog_prolenders;
import com.test.prolenders.util_prolenders.UserInfoUtil_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

public class PersonalCenterFragment_prolenders extends BaseFragment_prolenders {

    private LinearLayout csGroupLayout;
    private TextView csDescTv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = "ME";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_prolenders, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.fpc_refreshlayout);
        TextView mobileTv = view.findViewById(R.id.fpc_tv_mobile);

        csGroupLayout = view.findViewById(R.id.fpc_layout_customerservicegroup);
        csDescTv = view.findViewById(R.id.fpc_tv_customerservicedesc);

        view.findViewById(R.id.fpc_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog_prolenders(requireContext()).show());

        refreshLayout.setOnRefreshListener(() -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"REFRESH_CLICK","");
            refreshLayout.setRefreshing(false);
            HttpUtil_prolenders.getInstance().requestAppSInfo(requireActivity(),this::refreshCustomerservice);
        });

        mobileTv.setText(UserInfoUtil_prolenders.getInstance().getEncryptMobile());

        //feedback
        view.findViewById(R.id.fpc_view_feedback).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"FEEDBACK_CLICK","");
            startActivity(new Intent(requireContext(), FeedbackActivity_prolenders.class));
        });

        //privacypolicy
        view.findViewById(R.id.fpc_view_privacypolicy).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PRIVACY_POLICY_CLICK","");
            String agreementUrl = AppShowCustomize_prolenders.getInstance().getAgreementUrl();
            if (TextUtils.isEmpty(agreementUrl)) return;
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(agreementUrl)));
        });

        //setting
        view.findViewById(R.id.fpc_view_setting).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SETTING_CLICK","");
            startActivity(new Intent(requireContext(), SettingActivity_prolenders.class));
        });

        refreshCustomerservice();

    }

    public void refreshCustomerservice(){

        csGroupLayout.removeAllViews();

        String hint = AppShowCustomize_prolenders.getInstance().getCustomerServiceHint();
        String[] mobiles = AppShowCustomize_prolenders.getInstance().getMobiles();
        String[] emails = AppShowCustomize_prolenders.getInstance().getEmails();

        if (!TextUtils.isEmpty(hint)) csDescTv.setText(hint);

        for (int i=0;i<mobiles.length;i++){
            String mobile = mobiles[i];
            addMobile(mobile, (mobiles.length<=1 ? -1:i+1), !(i==mobiles.length-1 && emails.length==0));
        }

        for (int i=0;i<emails.length;i++){
            String email = emails[i];
            addEmail(email, (emails.length<=1 ? -1:i+1), !(i==emails.length-1));
        }
    }

    @SuppressLint("SetTextI18n")
    private void addMobile(String mobile, int num, boolean showLine){
        if (TextUtils.isEmpty(mobile)) return;

        View mobileItem = View.inflate(getContext(),R.layout.item_customer_service_prolenders,null);

        TextView titleTv = mobileItem.findViewById(R.id.ics_tv_title);
        TextView textTv = mobileItem.findViewById(R.id.ics_tv_text);
        TextView btnTv = mobileItem.findViewById(R.id.ics_tv_btn);
        View lineView = mobileItem.findViewById(R.id.ics_view_line);

        titleTv.setText(requireContext().getString(R.string.customer_service_hotline_title_prolenders) + (num>0 ? " "+num:""));
        textTv.setText(mobile);
        btnTv.setText(requireContext().getString(R.string.dial_prolenders));
        lineView.setVisibility(showLine ? View.VISIBLE:View.GONE);

        btnTv.setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_CALL","");
            Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile.trim()));//跳转到拨号界面，同时传递电话号码
            requireContext().startActivity(dialIntent);
        });

        csGroupLayout.addView(mobileItem);
    }

    @SuppressLint("SetTextI18n")
    private void addEmail(String email, int num, boolean showLine){
        if (TextUtils.isEmpty(email)) return;

        View emailItem = View.inflate(getContext(),R.layout.item_customer_service_prolenders,null);

        TextView titleTv = emailItem.findViewById(R.id.ics_tv_title);
        TextView textTv = emailItem.findViewById(R.id.ics_tv_text);
        TextView btnTv = emailItem.findViewById(R.id.ics_tv_btn);
        View lineView = emailItem.findViewById(R.id.ics_view_line);

        titleTv.setText(requireContext().getString(R.string.customer_service_email_title_prolenders) + (num>0 ? " "+num:""));
        textTv.setText(email);
        btnTv.setText(requireContext().getString(R.string.copy_prolenders));
        lineView.setVisibility(showLine ? View.VISIBLE:View.GONE);

        btnTv.setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"EMAIL_COPY","");
            ((ClipboardManager) requireContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", email.trim()));
            Toast.makeText(getContext(), requireContext().getString(R.string.copy_success_prolenders), Toast.LENGTH_SHORT).show();
        });

        csGroupLayout.addView(emailItem);
    }

}